using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public abstract class Item : MonoBehaviour{

	protected bool[,] spaceMatrix;
	protected int[] indexInInventory;
	protected Vector2 previousMousePosition;
	protected string description;
	
    [HideInInspector]
	public  Vector3 originalScale;
	public GameObject visualModelGUI;
	public GameObject visualModelWorld;
    public GameObject textGUI = null;
	public bool isBeingDragged;
	
	public bool wasResizedToFitPanel;
	
	public abstract void Use();
    public virtual void OnDrop()
    {
        Player.instance.GetSelectedCharacter().m_CharacterData.RemoveHotSkill(this);
    }

    /// <summary>
    /// Esta variable se usa cuando un item lo tiene el cadaver de un enemigo, al agarrar el item se le pone en null la variable m_AvailableLoot a este enemigo.
    /// </summary>
    public Enemy m_Holder = null;
	
	void Awake(){
		indexInInventory = new int[2]{-1,-1};
		isBeingDragged = false;		
		wasResizedToFitPanel = false;
		originalScale = this.transform.localScale;
	}
	public virtual void Update(){
		
		if (isBeingDragged){
			Drag();
		/*	if (Input.GetKeyDown(InputManager.HOTKEY1) )
				AssignItemToHotkey(InputManager.HOTKEY1);
			else if (Input.GetKeyDown(InputManager.HOTKEY2) )
				AssignItemToHotkey(InputManager.HOTKEY2);
			else if (Input.GetKeyDown(InputManager.HOTKEY3) )
				AssignItemToHotkey(InputManager.HOTKEY3);*/
		}
	
	}
	public string GetDescription(){
		return this.description;
	}	
	protected void FillSlots(){
		for (int row=0; row<spaceMatrix.GetLength(0); row++){
			for (int col=0; col<spaceMatrix.GetLength(1); col++){
				spaceMatrix[row,col] = true;	
			}
		}	
	}
/*	private void AssignItemToHotkey(KeyCode hotkeyPressed){
		
		if (hotkeyPressed == InputManager.HOTKEY1)
			CharacterDataManager.GetInstance().GetCurrentCharData()
			.hotSkills[0] = this;
		else if (hotkeyPressed == InputManager.HOTKEY2)
			CharacterDataManager.GetInstance().GetCurrentCharData()
			.hotSkills[1] = this;
		else if (hotkeyPressed == InputManager.HOTKEY3)
			CharacterDataManager.GetInstance().GetCurrentCharData()
			.hotSkills[2] = this;
	}*/
	private void Drag(){
		Vector3 newPosition = Input.mousePosition;
		Vector3 prevPosition = this.transform.position;
		//this.transform.position = Camera.main.ScreenToWorldPoint(newPosition);
		newPosition = 
			InventoryController.GetInstance().GetGUICamera().
				ScreenToWorldPoint(newPosition);
		newPosition.z = prevPosition.z;
		
		newPosition += Vector3.right * (this.transform.localScale.x 
		* visualModelGUI.transform.localScale.x)/2.0f 
		- Vector3.right*InventoryController.GetInstance().GetActiveInventory().slotscaleX/2;
		
		newPosition += Vector3.down * (this.transform.localScale.y 
		* visualModelGUI.transform.localScale.y)/2.0f
		- Vector3.down*InventoryController.GetInstance().GetActiveInventory().slotscaleY/2;
		
		this.transform.position = newPosition;
	}
	public virtual void OnRightClic(Vector3 clicPos){
		InventoryController.GetInstance().GetActiveInventory().StockItem(this);
	}
	public virtual void OnClic(Vector3 clicPos){
		if (isBeingDragged) {
			isBeingDragged = false;
			MouseController.instance.drawCursor = true;
			return;
		}
		this.isBeingDragged = true; 
		MouseController.instance.drawCursor = false;
	}
	public void ShowModelGUI(bool hasToShow){
		this.visualModelGUI.SetActive(hasToShow);
        if (textGUI != null)
        {
            this.textGUI.SetActive(hasToShow);
        }
	}
	public void ShowModelWorld(bool hasToShow){
		this.visualModelWorld.SetActive(hasToShow);
	}
	/// <summary>
	/// Este método le setea la posición que tiene el item en el inventario, sirve
	/// para cuando hay q retirarlo y limpiar los slots.
	/// </summary>
	/// <param name='row'>
	/// Recibe (row,col) del primer slot que ocupa: arriba-izquierda. 
	/// </param>
	public void SetPlaceInInventory(int row, int col){
		indexInInventory[0] = row;
		indexInInventory[1] = col;
	}
	/// <summary>
	/// Devuelve el indice (row,col) que ocupa este item en el inventario.
	/// </summary>
	public int [] GetIndexInInventory(){
		return indexInInventory;	
	}
	/// <summary>
	/// Devuelve el indice (row,col) que ocupa este item en el inventario.
	/// </summary>
	/// <param name='()'>
	/// Sin parametros = devuelve un int[2] con {fila,columna}
	/// </param> 
	/// <param name='(0)'> 
	/// 0 = row, devuelve solo la fila. 
	/// </param>
	/// <param name='(1)'> 
	/// 1 = col, devuelve solo la columna.
	/// </param>
	public int GetIndexInInventory(int index){
		return indexInInventory[index];	
	}
	public bool[,] GetSpaceMatrix(){ return spaceMatrix;}
}
